package com.david.appletree

import android.content.res.Resources
import android.graphics.*
import android.util.TypedValue
import java.util.*

class UserCareImgUtil {
    companion object{
        /**
         * 生成分享海报，以1125 2436为基准
         */
        @JvmStatic
        fun getShareBitmap(bg:Bitmap?, avatar:Bitmap?, avatarLabelIcon:Bitmap?, name:String, nameLabelIcon:Bitmap?, logoIcon:Bitmap, qrImg:Bitmap?) : Bitmap? {
            val screenWidth = 1125

            val bmp = Bitmap.createBitmap(screenWidth, 2436, Bitmap.Config.RGB_565)
            val canvas = Canvas(bmp)
            canvas.drawColor(Color.WHITE)

            val paint = Paint()
            paint.isAntiAlias = true //设置抗锯齿

            paint.style = Paint.Style.STROKE

            bg?.let {
                /*绘制背景*/
                val bgRect = Rect(0, 0, it.width, it.height)
                val bgDst = Rect(0, 0, screenWidth, 2028)
                canvas.drawBitmap(it, bgRect, bgDst, paint)
            }


            avatar?.let {
                /*绘制头像*/
                val avatarRect = Rect(0, 0, it.width, it.height)
                val avatarDst = Rect(72, 300, 288, 516)
                canvas.drawBitmap(it, avatarRect, avatarDst, paint)
            }


            /*绘制头像标签*/
            avatarLabelIcon?.let{
                val avatarLabelRect = Rect(0, 0, avatarLabelIcon.width, avatarLabelIcon.height)
                val avatarLabelDst = Rect(240, 468, 288, 516)
                canvas.drawBitmap(avatarLabelIcon, avatarLabelRect, avatarLabelDst, paint)
            }

            /*绘制下方logo*/
            val logoIconRect = Rect(0, 0, logoIcon.width, logoIcon.height)
            val logoIconDst = Rect(96, 2124, 390, 2196)
            canvas.drawBitmap(logoIcon, logoIconRect, logoIconDst, paint)

            qrImg?.let {
                /*绘制二维码*/
                val qrImgRect = Rect(0, 0, it.width, it.height)
                val qrImgDst = Rect(813, 2118, 1035, 2331)
                canvas.drawBitmap(it, qrImgRect, qrImgDst, paint)
            }


            /*绘制昵称*/
            paint.color = Color.parseColor("#24292B")
            paint.style = Paint.Style.FILL
            paint.textSize = dp2px(24f)
            canvas.drawText(name,336f, 390f,  paint)

            nameLabelIcon?.let {
                /*绘制昵称标签*/
                val nameLabelRect = Rect(0, 0, it.width, it.height)
                val nameLabelWidth = (339f + paint.measureText(name)).toInt()
                val nameLabelDst = Rect(nameLabelWidth + 2, 345, nameLabelWidth + 132, 387)
                canvas.drawBitmap(it, nameLabelRect, nameLabelDst, paint)
            }


            /*绘制日期*/
            paint.textSize = dp2px(18f)
            canvas.drawText("${Calendar.getInstance().get(Calendar.YEAR)}/${
                Calendar.getInstance().get(
                    Calendar.MONTH) + 1}/${Calendar.getInstance().get(Calendar.DATE)}",336f, 480f,  paint)

            paint.textSize = dp2px(12f)
            canvas.drawText("长按识别二维码",126f, 2265f,  paint)

            canvas.drawText("马上拥有你的专属定制极氪001",126f, 2316f,  paint)

            /*绘制竖线*/
            paint.color = Color.parseColor("#FF5800")
            paint.strokeWidth = dp2px(3f)
            canvas.drawLine(96f,2235f, 96f, 2319f, paint)
            return bmp
        }

        fun dp2px(dp : Float): Float{
            val r = Resources.getSystem()
            return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, r.displayMetrics)
        }
    }
}